{extend name="$base" /}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/member_list.css" />
<script type="text/javascript" src="__STATIC__/My97DatePicker/WdatePicker.js"></script>
<style>
	.count-table .title{
		font-weight:bold;
		margin-bottom:10px;
	}
	.count-table span{
		display:inline-block;
		width:16%;
	}
	.table-class{border-top: 1px solid #E1E6F0}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th style="text-align: left;width: 20%;">
			<button class="btn-common" onclick="add_agent()" >添加区域代理</button>
		</th>
		<th width="50%">
			<span>发生时间：</span>
			<input type="text" id='startDate' placeholder="请选择开始日期" class="input-medium input-common  middle" onclick="WdatePicker()"/>
			&nbsp;-&nbsp;
			<input type="text" id='endDate' placeholder="请选择结束日期" class="input-medium input-common  middle" onclick="WdatePicker()" />
			<!-- 更多搜索按钮 -->
			<button class="btn-common-white more-search"><i class="fa fa-angle-down"></i></button>
			<button type="button" onclick="searchData()" value="搜索" class="btn-common" >搜索</button>
			<!-- 更多搜索下拉框 -->
			<div class="more-search-container">
				<dl>
					<dt>相关描述：</dt>
					<dd>
						<input type="text" id='search_text' placeholder="请输入相关描述" class="input-common  middle" />
					</dd>
				</dl>
				<dl>
					<dt>代理类型：</dt>
					<dd>
						<select id="agent_type" class="select-common  middle">
							<option value="">全部</option>
							<option value="1">省</option>
							<option value="2">市</option>
							<option value="3">县</option>
						</select>
					</dd>
				</dl>
				<dl>
					<dt></dt>
					<dd><button onclick="searchData()" class="btn-common">搜索</button></dd>
				</dl>
			</div>
		</th>
	</tr>
</table>
<table class="table-class" >
	<thead>
	<tr align="center">
		<th ><i class="checkbox-common"><input type="checkbox" ></i></th>
		<th align="left">会员昵称</th>
		<th align="left">代理类型</th>
		<th align="left">省</th>
		<th align="left">市</th>
		<th align="left">县</th>
		<th align="center">说明</th>
		<th>时间</th>
		<th>操作</th>
	</tr>
	</thead>
	<tbody id="my-table"></tbody>
</table>
<input type="hidden" id="member_id" value="{$member_id}"/>


<!-- 添加会员 -->
<div class="modal fade hide" id="add_agent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h3>添加区域代理</h3>
			</div>
			<div class="modal-body">
				<div class="modal-infp-style">
					<table class="modal-tab">
						<tr>
							<td style="width:20%;"><span class="required">*</span>用户名</td>
							<td colspan='3'>
								<input type="text" id="username" class="input-common" />
								<span id="username_verify"></span>
								<input type="hidden" value="不存在" id="isset_username" class="input-common" />
							</td>
						</tr>
						<tr>
							<td>省</td>
							<td colspan='3'>
								<select name="province" id="province" onchange="changeProvince();"  class="select-common" style="width:150px!important;">
									<option value="">请选择省</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>市</td>
							<td colspan='3'>
								<select name="city" id="city" onchange="changeCity();"  class="select-common" style="width:150px!important;">
									<option value="">请选择市</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>县</td>
							<td colspan='3'>
								<select name="district" id="district"  class="select-common" style="width:150px!important;">
									<option value="-1">请选择区/县</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>代理类型</td>
							<td colspan='3'>
								<select name="type" id="type"  class="select-common" style="width:150px!important;">
									<option value="1">省</option>
									<option value="2">市</option>
									<option value="3">县</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>备注</td>
							<td colspan='3'>
								<textarea id="remark" class="textarea-common"></textarea>
							</td>
						</tr>

					</table>
				</div>
			</div>

			<div class="modal-footer">
				<button class="btn-common btn-big" onclick="addAgent()">保存</button>
				<button class="btn-common-cancle btn-big" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>

</div>

{/block}
{block name="script"}
<script type="text/javascript">
	function LoadingInfo(page_index) {

		var search_text = $("#search_text").val();
		var start_date = $("#startDate").val();
		var end_date = $("#endDate").val();
		var member_id = $("#member_id").val();
		var agent_type = $("#agent_type").val();
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/member/agentList')}",
			data : {
				"page_index" : page_index,
				"page_size" : $("#showNumber").val(),
				"search_text":search_text,
				"member_id":member_id,
				"start_date":start_date,
				"end_date":end_date,
				"agent_type":agent_type
			},
			success : function(data) {
				if (data["data"].length > 0) {
					$("#my-table").empty();
					for (var i = 0; i < data["data"].length; i++) {
						var html = '';
						html += '<tr align="center">';
						html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data["data"][i]["regain_id"] +'" ></i></td>';
						html += '<td align="left">'+data["data"][i]["nick_name"]+'<br>'+data["data"][i]["user_name"]+'</td>';
						html += '<td align="left">'+data["data"][i]["agent_type"]+'</td>';
						html += '<td align="left">'+data["data"][i]["agent_province_name"]+'</td>';
						html += '<td align="left">'+ data["data"][i]["agent_city_name"] +'</td>';
						html += '<td align="left">'+ data["data"][i]["agent_district_name"] +'</td>';
						html += '<td align="left">'+ data["data"][i]["remark"] +'</td>';
						html += '<td align="center">'+ timeStampTurnTime(data["data"][i]["create_time"]) +'</td>';
						html += '<td align="center"><a href="javascript:deleteAgent('+data['data'][i]['agent_id']+');">删除</a></td>';

						html += '</tr>';
						html += '<input type="hidden" id="member_id" value="{$member_id}"/>';
						$("#my-table").append(html);
					}
				} else {
					var html = '<tr align="center"><td colspan="8">暂无符合条件的数据记录</td></tr>';
					$("#my-table").html(html);
				}
				initPageData(data["page_count"],data['data'].length,data['total_count']);
				$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
			}
		});
	}

	function searchData(){
		LoadingInfo(1);
	}

	//点击显示更多搜索
	$(".more-search").click(function(){
		$(".more-search-container").slideToggle();
	})


	//添加会员弹出
	function add_agent(){
		$("#add_agent").modal("show");
	}

	function init() {
		getProvince();
	}


	function getProvince(province_id){
		$.ajax({
			type : "post",
			url :"{:__URL('ADMIN_MAIN/order/getProvince')}",
			dataType : "json",
			success : function(data) {
				if (data != null && data.length > 0) {
					var str = "";
					for (var i = 0; i < data.length; i++) {
						str += '<option value="'+data[i].province_id+'">'+data[i].province_name+'</option>';
					}
					$("#province").append(str);
					$('.select-common').selectric();
				}
			}
		});
	}
	//选择省份弹出市区
	function changeProvince(city_id) {
		var id = $("#province").find("option:selected").val();
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/order/getcity')}",
			dataType : "json",
			data : {
				"province_id" : id
			},
			async:false,
			success : function(data) {
				if (data != null && data.length > 0) {
					var str = "<option value='-1'>请选择市</option>";
					for (var i = 0; i < data.length; i++) {
						if(city_id ==data[i].city_id){
							str += '<option value="'+data[i].city_id+'" selected="selected">'+data[i].city_name+'</option>';
						}else{
							str += '<option value="'+data[i].city_id+'">'+data[i].city_name+'</option>';
						}
					}
					$("#city").html(str);
					$("#district").html('<option value="0" selected="selected">请选择区</option>');
					$('.select-common').selectric();
				}

			}
		});
	};

	//选择市区弹出区域
	function changeCity(district_id) {
		var id = $("#city").find("option:selected").val();
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/order/getdistrict')}",
			dataType : "json",
			data : {
				"city_id" : id
			},
			async:false,
			success : function(data) {
				if (data != null && data.length > 0) {
					var str = "<option value='-1'>请选择区</option>";
					for (var i = 0; i < data.length; i++) {
						if(district_id == data[i].district_id){
							str += '<option value="'+data[i].district_id+'" selected="selected">'+data[i].district_name+'</option>';
						}else{
							str += '<option value="'+data[i].district_id+'">'+data[i].district_name+'</option>';
						}
					}
					$("#district").html(str);
					$('.select-common').selectric();
				}

			}
		});
	}

	//添加会员
	function addAgent(){
		var username = $("#username").val();
		var province = $("#province").val();
		var city = $("#city").val();
		var district = $("#district").val();
		var type = $("#type").val();
		var remark = $("#remark").val();


		if (username == '') {
			showTip('用户名不能为空','warning');
			return;
		}

		$.ajax({
			type : "post",
			url : __URL("ADMIN_MAIN/member/addagent"),
			data : {
				'username' : username,
				'province' :province,
				'city' : city,
				'district' : district,
				'type':type,
				'remark':remark
			},
			success : function(data) {
				if (data['code'] > 0) {
					showTip(data['message'],'success');
					$("#add_agent").modal("hide");
					LoadingInfo(getCurrentIndex(1,'#productTbody'));
				} else {
					showTip(data['message'],'error');
					flag = false;
				}
			}
		});
	}

	function deleteAgent(agent_id){
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/member/deleteMemberAgent')}",
			data : { 'agent_id' : agent_id },
			success : function(data) {
				if (data['code'] > 0) {
					showTip(data['message'],'success');
					location.href=__URL("ADMIN_MAIN/member/agentlist");
				} else {
					showTip(data['message'],'error');
				}
			}
		});
	}
	init();
</script>
{/block}